package com.maotaiapp.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotaiapp.bean.BuyBean;
import com.maotaiapp.bean.InvioceInfo;
import com.maotaiapp.bean.KuCun;
import com.maotaiapp.bean.MaotaiAppCookie;

public class MaotaiAppHttpClientUtil {
	private RequestConfig requestConfig = RequestConfig.custom()
			.setSocketTimeout(150000).setConnectTimeout(150000)
			.setConnectionRequestTimeout(150000).build();;

	private static MaotaiAppHttpClientUtil instance = null;

	private MaotaiAppHttpClientUtil() {
	}

	public static MaotaiAppHttpClientUtil getInstance() {
		if (instance == null) {
			instance = new MaotaiAppHttpClientUtil();
		}
		return instance;
	}

	/**
	 * 发送 get请求
	 * 
	 * @param httpUrl
	 * @throws Exception 
	 */
	public MaotaiAppCookie sendHttpGet(String httpUrl) throws Exception {
		HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
		setHttpGetHeader(httpGet);
		return sendHttpGet(httpGet);
	}
	public MaotaiAppCookie sendHttpGetUUID(String httpUrl,MaotaiAppCookie mc,String UUID1,String UUID2,RequestConfig config) throws Exception {
		HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
		setHttpGetHeaderUUID(httpGet,UUID1,UUID2);
		return sendHttpGetUUID(httpGet,mc,config);
	}
	/**
	 * 发送 get请求Https
	 * 
	 * @param httpUrl
	 */
	/**
	 * 发送 get请求Https
	 * 
	 * @param httpUrl
	 */
	/**
	 * 发送Get请求
	 * 
	 * @param httpPost
	 * @return
	 * @throws Exception 
	 */
	
	private MaotaiAppCookie sendHttpGetUUID(HttpGet httpGet,MaotaiAppCookie mc,RequestConfig config) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		HttpEntity entity = null;
		try {
			// 创建默认的httpClient实例.
			httpClient = HttpClients.createDefault();
			
			httpGet.setConfig(config);
			// 执行请求
			response = httpClient.execute(httpGet);
			Header[] headers = response.getAllHeaders();
			if (headers != null && headers.length > 0) {
				for (Header hea : headers) {
					if(hea.getName().contains("Set-Cookie")){
						System.out.println(hea.getName() + "::::::::::::::"
								+ hea.getValue());
						if(hea.getValue().contains("SERVERID")){
							mc.setServerId(hea.getValue());
						}
						if(hea.getValue().contains("Vshop-Member")){
							mc.setVshopMember(hea.getValue());
						}
						if(hea.getValue().contains("SessionId")){
							mc.setSessionId(hea.getValue());
						}
						if(hea.getValue().contains("acw_tc")){
							mc.setAcwTc(hea.getValue());
						}
					}
				}
			}
			entity = response.getEntity();
			
			EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("网络访问异常");
		} finally {
			try {
				// 关闭连接,释放资源
				if (response != null) {
					response.close();
				}
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mc;
	}
	private MaotaiAppCookie sendHttpGet(HttpGet httpGet) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		HttpEntity entity = null;
		MaotaiAppCookie mc = new MaotaiAppCookie();
		try {
			// 创建默认的httpClient实例.
			httpClient = HttpClients.createDefault();
			
			httpGet.setConfig(requestConfig);
			// 执行请求
			response = httpClient.execute(httpGet);
			Header[] headers = response.getAllHeaders();
			mc = new MaotaiAppCookie();
			if (headers != null && headers.length > 0) {
				for (Header hea : headers) {
					if(hea.getName().contains("Set-Cookie")){
						System.out.println(hea.getName() + "::::::::::::::"
								+ hea.getValue());
						if(hea.getValue().contains("SERVERID")){
							mc.setServerId(hea.getValue());
						}
						if(hea.getValue().contains("Vshop-Member")){
							mc.setVshopMember(hea.getValue());
						}
						if(hea.getValue().contains("SessionId")){
							mc.setSessionId(hea.getValue());
						}
						if(hea.getValue().contains("acw_tc")){
							mc.setAcwTc(hea.getValue());
						}
					}
				}
			}
			entity = response.getEntity();
			EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("网络访问异常");
		} finally {
			try {
				// 关闭连接,释放资源
				if (response != null) {
					response.close();
				}
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mc;
	}
	/**
	 * 发送Get请求
	 * 
	 * @param httpPost
	 * @return
	 */
	
	
	private HttpGet setHttpGetHeaderUUID(HttpGet httpGet,String UUID1,String UUID2) {
		httpGet.addHeader("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		httpGet.addHeader("Accept-Encoding", "gzip,deflate");
		httpGet.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
		httpGet.addHeader("Connection", "keep-alive");
		httpGet.addHeader("Host", "www.cmaotai.com");
		httpGet.addHeader("Origin", "https://www.cmaotai.com");
		httpGet.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Linux; Android 4.4.4; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36[android/1.0.21/"+UUID1+"/"+UUID2+"]");
		return httpGet;
	}
	private HttpGet setHttpGetHeader(HttpGet httpGet) {
		httpGet.addHeader("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		httpGet.addHeader("Accept-Encoding", "gzip,deflate");
		httpGet.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
		httpGet.addHeader("Connection", "keep-alive");
		httpGet.addHeader("Host", "www.cmaotai.com");
		httpGet.addHeader("Origin", "https://www.cmaotai.com");
		httpGet.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Linux; Android 4.4.4; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36[android/1.0.21/36ecb0f48aeee595d31cf0b2c7f0d724/a08229b288a99b46dceae14dbbb6bbfb]");
		return httpGet;
	}
	private HttpPost setHttpPostHeader(HttpPost httpPost,  MaotaiAppCookie mc) {
		httpPost.addHeader("Accept",
				"application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Referer",
				"https://www.cmaotai.com/ysh5/page/LoginRegistr/userRegistr1.html");
		httpPost.addHeader("Accept-Encoding", "gzip,deflate");
		httpPost.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "www.cmaotai.com");
		httpPost.addHeader("Origin", "https://www.cmaotai.com");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Linux; Android 4.4.4; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36[android/1.0.21/36ecb0f48aeee595d31cf0b2c7f0d724/a08229b288a99b46dceae14dbbb6bbfb]");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		if(!"".equals(mc.getAcwTc())){
			System.out.println(mc.getAcwTc()+";"+mc.getServerId());
			httpPost.addHeader("Cookie", mc.getAcwTc()+";"+mc.getServerId());
		}
		
		return httpPost;
	}
	private HttpPost setHttpPostHeaderUUID(HttpPost httpPost,  MaotaiAppCookie mc,String UUID1,String UUID2) {
		httpPost.addHeader("Accept",
				"application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Referer",
				"https://www.cmaotai.com/ysh5/page/LoginRegistr/userRegistr1.html");
		httpPost.addHeader("Accept-Encoding", "gzip,deflate");
		httpPost.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "www.cmaotai.com");
		httpPost.addHeader("Origin", "https://www.cmaotai.com");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Linux; Android 4.4.4; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36[android/1.0.21/"+UUID1+"/"+UUID2+"]");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		if(!"".equals(mc.getAcwTc())){
			System.out.println(mc.getAcwTc()+";"+mc.getServerId());
			httpPost.addHeader("Cookie", mc.getAcwTc()+";"+mc.getServerId());
		}
		
		return httpPost;
	}
	private HttpPost setHttpPostHeader(HttpPost httpPost,  MaotaiAppCookie mc, String referer) {
		httpPost.addHeader("Accept",
				"application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Referer",
				referer);
		httpPost.addHeader("Accept-Encoding", "gzip,deflate");
		httpPost.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "www.cmaotai.com");
		httpPost.addHeader("Origin", "https://www.cmaotai.com");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Linux; Android 4.4.4; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36[android/1.0.21/36ecb0f48aeee595d31cf0b2c7f0d724/a08229b288a99b46dceae14dbbb6bbfb]");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		if(!"".equals(mc.getAcwTc())){
			httpPost.addHeader("Cookie", mc.getAcwTc()+";"+mc.getServerId()+";"+mc.getVshopMember()+";"+mc.getSessionId());
		}
		
		return httpPost;
	}
	private HttpPost setHttpPostHeaderUUID(HttpPost httpPost,  MaotaiAppCookie mc, String referer,String UUID1,String UUID2) {
		httpPost.addHeader("Accept",
				"application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Referer",
				referer);
		httpPost.addHeader("Accept-Encoding", "gzip,deflate");
		httpPost.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "www.cmaotai.com");
		httpPost.addHeader("Origin", "https://www.cmaotai.com");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Linux; Android 4.4.4; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36[android/1.0.21/"+UUID1+"/"+UUID2+"]");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		if(!"".equals(mc.getAcwTc())){
			httpPost.addHeader("Cookie", mc.getAcwTc()+";"+mc.getServerId()+";"+mc.getVshopMember());
		}
		
		return httpPost;
	}
	 /** 
     * 发送 post请求 
     * @param httpUrl 地址 
	 * @throws Exception 
     */  
    public MaotaiAppCookie sendHttpPostVShop(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeader(httpPost, mc,referer);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        }
        return sendHttpPostVShop(httpPost,mc);  
    }
    public MaotaiAppCookie sendHttpPostVShopUUID(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer,String UUID1,String UUID2,RequestConfig config) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeaderUUID(httpPost, mc,referer,UUID1,UUID2);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace(); 
            throw new Exception("网络访问异常");
        }
        return sendHttpPostVShopUUID(httpPost,mc, config);  
    }
    
    public InvioceInfo sendHttpPostGetInvioceInfo(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeader(httpPost, mc,referer);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        }
        return sendHttpPostGetInvioceInfo(httpPost,mc);  
    }
    
    public KuCun sendHttpPostKuCunUUID(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer,String UUID1,String UUID2,RequestConfig config) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeaderUUID(httpPost, mc, referer, UUID1, UUID2);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace(); 
            throw new Exception("网络访问异常");
        }
        return sendHttpPostKuCunUUID(httpPost,mc,config);  
    }
    public KuCun sendHttpPostKuCun(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeader(httpPost, mc,referer);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace(); 
            throw new Exception("网络访问异常");
        }
        return sendHttpPostKuCun(httpPost,mc);  
    }
    public BuyBean sendHttpPostBuy(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeader(httpPost, mc,referer);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        }
        return sendHttpPostBuy(httpPost,mc);  
    }
    public BuyBean sendHttpPostBuyUUID(String httpUrl, MaotaiAppCookie mc, Map<String, String> maps, String referer,String UUID1,String UUID2,RequestConfig config) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost    
        //设置请求头
        httpPost = setHttpPostHeaderUUID(httpPost, mc, referer, UUID1, UUID2);
     // 创建参数队列    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        }
        return sendHttpPostBuyUUID(httpPost,mc,config);  
    }
    /** 
     * 发送 post请求 
     * @param httpUrl 地址 
     * @param maps 参数 
     * @throws Exception 
     */  
    
    private MaotaiAppCookie sendHttpPostVShopUUID(HttpPost httpPost, MaotaiAppCookie mc, RequestConfig config) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(config);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	mc.setContent(responseContent);
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    }
            	}else if(jObject.getIntValue("code")==1){
            		return null;
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return mc;  
    }
    private MaotaiAppCookie sendHttpPostVShop(HttpPost httpPost, MaotaiAppCookie mc) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(requestConfig);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	mc.setContent(responseContent);
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    }
            	}else if(jObject.getIntValue("code")==1){
            		return null;
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return mc;  
    }  
    
    private InvioceInfo sendHttpPostGetInvioceInfo(HttpPost httpPost, MaotaiAppCookie mc) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        InvioceInfo kc = new InvioceInfo();
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(requestConfig);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	//解析JSON
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		//解析库存
            		JSONArray data = (JSONArray)jObject.get("data");
            		if(data.size()>0){
            			JSONObject invoiceInfo = (JSONObject)data.get(0);
            			int invioceId = invoiceInfo.getIntValue("Id");
            			kc.setInvioceId(invioceId+"");
            		}
            		//解析Head
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    	kc.setMc(mc);
                    }
            	}else if(jObject.getIntValue("code")==1){
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return kc;  
    }
    
    private KuCun sendHttpPostKuCunUUID(HttpPost httpPost, MaotaiAppCookie mc,RequestConfig config) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        KuCun kc = new KuCun();
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(config);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	//解析JSON
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		//解析库存
            		JSONObject data = (JSONObject)jObject.get("data");
            		JSONObject address = (JSONObject)data.get("address");
            		JSONObject stock = (JSONObject)data.get("stock");
            		int addressId = address.getIntValue("ID");
            		int quotasStockCount = stock.getIntValue("QuotasStockCount");
            		String sid = stock.getString("Sid");
            		//"HasSelfDelivery":false 是否能自提
            		boolean hasSelfDelivery = stock.getBooleanValue("HasSelfDelivery");
            		kc.setQuotasStockCount(quotasStockCount);
            		kc.setSid(sid);
            		kc.setAddressId(addressId);
            		kc.setHasSelfDelivery(hasSelfDelivery);
            		
            		//解析Head
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    	kc.setMc(mc);
                    }
            	}else if(jObject.getIntValue("code")==1){
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return kc;  
    }
    private KuCun sendHttpPostKuCun(HttpPost httpPost, MaotaiAppCookie mc) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        KuCun kc = new KuCun();
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(requestConfig);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	//解析JSON
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		//解析库存
            		JSONObject data = (JSONObject)jObject.get("data");
            		JSONObject address = (JSONObject)data.get("address");
            		JSONObject stock = (JSONObject)data.get("stock");
            		int addressId = address.getIntValue("ID");
            		int quotasStockCount = stock.getIntValue("QuotasStockCount");
            		String sid = stock.getString("Sid");
            		kc.setQuotasStockCount(quotasStockCount);
            		kc.setSid(sid);
            		kc.setAddressId(addressId);
            		//解析Head
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    	kc.setMc(mc);
                    }
            	}else if(jObject.getIntValue("code")==1){
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return kc;  
    }
    
    private BuyBean sendHttpPostBuyUUID(HttpPost httpPost, MaotaiAppCookie mc,RequestConfig config) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        BuyBean kc = new BuyBean();
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(config);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	//解析JSON
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		//解析库存
            		JSONObject data = (JSONObject)jObject.get("data");
            		String cid = data.getString("Cid");
            		kc.setCid(cid);
            		//解析Head
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    	kc.setMc(mc);
                    }
            	}else if(jObject.getIntValue("code")==1){
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return kc;  
    }
    private BuyBean sendHttpPostBuy(HttpPost httpPost, MaotaiAppCookie mc) throws Exception {  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;  
        String responseContent = null;  
        BuyBean kc = new BuyBean();
        try {  
            // 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault(); 
            httpPost.setConfig(requestConfig);  
            // 执行请求  
            response = httpClient.execute(httpPost); 
            
            entity = response.getEntity();  
            if(entity!=null){
            	responseContent = EntityUtils.toString(entity, "UTF-8");  
            	System.out.println(responseContent);
            	//解析JSON
            	JSONObject jObject = JSONObject.parseObject(responseContent);
            	if(jObject.getIntValue("code")==0){
            		//解析库存
            		JSONObject data = (JSONObject)jObject.get("data");
            		String cid = data.getString("Cid");
            		kc.setCid(cid);
            		//解析Head
            		Header[] headers = response.getAllHeaders();
                    if(headers!=null&&headers.length>0){
                    	for (Header hea : headers) {
        					if(hea.getName().contains("Set-Cookie")){
        						if(hea.getValue().contains("SERVERID")){
        							mc.setServerId(hea.getValue());
        						}
        						if(hea.getValue().contains("Vshop-Member")){
        							mc.setVshopMember(hea.getValue());
        						}
        						if(hea.getValue().contains("SessionId")){
        							mc.setSessionId(hea.getValue());
        						}
        						if(hea.getValue().contains("acw_tc")){
        							mc.setAcwTc(hea.getValue());
        						}
        					}
        				}
                    	kc.setMc(mc);
                    }
            	}else if(jObject.getIntValue("code")==1){
            	}
            }
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception("网络访问异常");
        } finally {  
            try {  
                // 关闭连接,释放资源  
                if (response != null) {  
                    response.close();  
                }  
                if (httpClient != null) {  
                    httpClient.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return kc;  
    }
}